package cn.bluethink.eguan.utils;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import cn.bluethink.eguan.model.okrtask.EgTimingType;

/**
 * 目标标签计算工具
 * 
 * @author 苏州中科蓝迪公司所有(c)2016-2021
 * @version 0.3.0
 * @brief 驿馆+产品
 * @note 修订历史： 1.giszyj@126.com于2019/02/18设计并构建初始版本v0.3.2 2.wanglei于2019/02/19修订
 */
public class OkrTaskUtil {

    /**
     * 根据目标标识，截止时间，季度年的开始月份获取相应的标签label
     * 
     * @param timing
     *            目标标识
     * @param etime
     *            截止时间
     * @param bMonth
     *            季度年的开始月份
     * @return
     * @throws Exception
     */
    public String label(Integer timing, Date etime, Integer bMonth) {
        Calendar c = Calendar.getInstance();
        c.setTime(etime);
        int y = c.get(Calendar.YEAR);
        int q = 0;
        int m = c.get(Calendar.MONTH) + 1;// 当前月数
        int w = c.get(Calendar.DAY_OF_WEEK_IN_MONTH);
        if (m - bMonth < 0) { // 如果当前月小于起始月
            y = y - 1;
            // 求当前月到年底经过的季度数和剩余月数
            int fullQ = (13 - bMonth) / 3;
            int remainM = (13 - bMonth) % 3;
            // fullQ + (剩余月数+当前月取整) 即为所求当前月所在的季度
            q = (m + remainM + 2) / 3 + fullQ;
            m = (m + remainM) % 3;
            // 余数为0的表示当前月正好为所属季度的第三个月
            m = m == 0 ? 3 : m;
        } else {
            q = (m - bMonth + 3) / 3;
            m = (m - bMonth + 1) % 3;
            m = m == 0 ? 3 : m;
        }
        String label = null;
        switch (timing) {
        case EgTimingType.YEAR_TARGET:
            label = "Y" + y;
            break;
        case EgTimingType.QUARTER_TARGET:
            label = "Y" + y + "Q" + q;
            break;
        case EgTimingType.MONTH_TARGET:
            label = "Y" + y + "Q" + q + "M" + m;
            break;
        case EgTimingType.WEEK_TARGET:
            label = "Y" + y + "Q" + q + "M" + m + "W" + w;
            break;
        default:
            break;
        }
        return label;
    }

    /**
     * 在给定的目标截止时间，季度年的开始月份，获取该目标的上一级目标的起止时间（区间时间）
     * 
     * @param timing
     *            上一级目标标识
     * @param etime
     *            该目标的截止时间
     * @param bMonth
     *            季度年的开始月
     * @return
     * @throws Exception
     */
    public List<Date> intervalDate(Integer timing, Date etime, Integer bMonth) throws Exception {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(etime);
        int y = calendar.get(Calendar.YEAR);
        int m = calendar.get(Calendar.MONTH) + 1;// 当前月数
        int sy = y;
        int sm = m;
        int ey = y;
        int em = m;
        List<Date> dates = null;
        switch (timing) {
        case EgTimingType.YEAR_TARGET: {
            if (m - bMonth < 0) {
                sy = y - 1;
            } else {
                ey = y + 1;
            }
            dates = new ArrayList<Date>();
            calendar.set(sy, bMonth - 1, 1);
            Date sdate = new Date(calendar.getTimeInMillis());
            if (bMonth == 1) {
                // 只有季度年的开始月为1月，季度年的截止月为12月
                calendar.set(ey, 11, 1);
            } else {
                calendar.set(ey, bMonth - 2, 1);
            }
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            Date edate = new Date(calendar.getTimeInMillis());
            dates.add(sdate);
            dates.add(edate);
        }
            break;
        case EgTimingType.QUARTER_TARGET: {
            if (m - bMonth < 0) {
                y = y - 1;
                int remainM = (13 - bMonth) % 3;
                m = (m + remainM) % 3;
                m = m == 0 ? 3 : m;
                if (sm >= m) {
                    sm = sm - m + 1;
                    em = sm + 2;
                } else {
                    int temp = sm;
                    sy = y;
                    sm = 12 - (m - sm) + 1;
                    em = temp + (3 - m);
                }
            } else {
                m = (m - bMonth + 1) % 3;
                m = m == 0 ? 3 : m;
                if (12 - sm >= 3 - m) {
                    sm = sm - m + 1;
                    em = sm + 2;
                } else {
                    sm = sm - m + 1;
                    ey = y + 1;
                    em = 3 - m;
                }
            }
            dates = new ArrayList<Date>();
            calendar.set(sy, sm - 1, 1);
            Date sdate = new Date(calendar.getTimeInMillis());
            calendar.set(ey, em - 1, 1);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            Date edate = new Date(calendar.getTimeInMillis());
            dates.add(sdate);
            dates.add(edate);
        }
            break;
        case EgTimingType.MONTH_TARGET: {
            dates = new ArrayList<Date>();
            calendar.set(y, m - 1, 1);
            Date sdate = new Date(calendar.getTimeInMillis());
            calendar.set(y, m - 1, 1);
            calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
            Date edate = new Date(calendar.getTimeInMillis());
            dates.add(sdate);
            dates.add(edate);
        }
            break;
        default:
            break;
        }
        return dates;
    }

}
